/**
 * for user
 */
$(function(){
	getUserLists(1);
	// Check all checkboxes when the one in a table head is checked:			
	$('.check-all').click(function(){
		$("#datatable tbody tr").find("input[type='checkbox']").attr('checked', $(this).is(':checked'));   
	});
	
	$(".form-layout .button").click(function(){
		//var dataArray = $("form").serializeArray();		
		getUserLists(1, null, null, getParamer());
	})

	$(".batchOp").click(function(){
		if($("#batchOp").val() == "Delete"){
			var array = [];
			$("[name='userId']:checked").each(function(i){
				array[i] = $(this).val();
			})
			
			if(array.length > 0){
				$.ajax({
					url: ctx+"/user/delete.do",
					data : {userIds : $.toJSON(array)},
					success: function(){
						getUserLists(1, null, null, getParamer());
					}
				})
				$(".check-all").attr("checked", false);
			}else{
				showInfo("May selected many.");
			}
		}else{
			showInfo("Please choose a type");
		}
	})
	$(".sort").click(function(){
		var orderBy = $(this).attr("title");
		var orderType = "asc";
		if($(this).hasClass("asc")){
			orderType = "desc";
			$(this).addClass("desc").removeClass("asc");
		}else{
			$(this).addClass("asc").removeClass("desc");
		}
		getUserLists(1, orderType, orderBy, getParamer());
	})
})
function getParamer(){
	var data = [];
	var userTitle = {};
	userTitle["property"] = "userTitle";
	userTitle["value"] = $("#userTitle").val();
	userTitle["condition"] = "fl";
	if(isNotBlank($("#userTitle").val())){
		data[0] = userTitle;
	}
	
	var userStatus = {};	
	userStatus["property"] = "userStatus";
	userStatus["value"] = $("#userStatus").val();	
	userStatus["condition"] = "eq";
	if(isNotBlank($("#userStatus").val())){
			data[data.length] = userStatus;		
	}
	
	var languageId = {};
	languageId["property"] = "l.languageId";
	languageId["value"] = $("#languageId").val();
	languageId["condition"] = "eq";
	if(isNotBlank($("#languageId").val())){
		data[data.length] = languageId;
	}
	
	return $.toJSON(data);
}
function getUserLists(pageNo, orderType, orderBy, paramdata){	
			if(!pageNo){
				pageNo = 1;
			}
			if(!orderType){
				orderType = "desc";
			}
			if(!orderBy){
				orderBy = "isTop";
			}
			if(!paramdata){
				paramdata="[]";
			}			
			pagination({
				url:ctx+"/user/search.do",
				pageNo:pageNo, 
				orderType : orderType, 
				orderBy : orderBy, 
				rules : paramdata,
				callback: function(data){
					generateUserLists(data);
					paginationClick();
				}
			})
		}

		function generateUserLists(data){
			$("#datatable tbody").empty();
			if(data.result){
				$.each(data.result, function(index, rowdata){						
					var editop = $('<a href="javascript:void(0)" title="Edit">編輯</a>');
					var deleteop = $('<a href="javascript:void(0)" title="Delete">刪除</a>');
					var tr = $("<tr>")
					.append($("<td>").append($("<input type='checkbox'>").attr("name", "userId").val(rowdata.appUserId)).addClass("text-center"))
					.append($("<td>").text(rowdata.userName))
					.append($("<td>").html(rowdata.permissionNamesHtml))
					.append($("<td>").append(editop).addClass("text-center op-1"))
					.append($("<td>").append(deleteop).addClass("text-center op-2"));
					$("#datatable tbody").append(tr);
				})
				operation();
			}
		    // Alternating table rows:					
			$('#datatable tbody tr:even').addClass("alt-row"); // Add class "alt-row" to even table rows
		}
        
		function operation(){
			$("#datatable tbody tr").each(function(){
				var userId = $(this).find(":checkbox").val();
				$(this).find("a").on("click", function(){
					var title = $(this).attr("title");
					if('Edit'.equals(title)){
						location.href=ctx+"/user/edit/"+userId+".do";
					}else if("Delete".equals(title)){
						showMessage({message:"Are you sure delete this user?"},{OK:function(){
							$.ajax({
								url: ctx+"/user/delete/"+userId+".do",
								success: function(){
									getUserLists(1);
								}
							})						
						}, Cancel: function(){						
						}});
					}			
				})
			})
		}
       function paginationClick(){
    	   $(".pagination a:not(.disabled):not(.info):not(.current)").on("click",function(){
    		   var pageNo = $(this).attr("data-page");
    		   getUserLists(pageNo);
    	   })
       }       